//
//  KRRepairReportVC.m
//  KEREN
//
//  Created by MW on 2023/4/21.
//

#import "KRRepairReportVC.h"
#import "KRRepairReportDetailVC.h"
#import "RepairModel.h"
#import "KRNodataView.h"
@interface KRRepairReportVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArrM;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) int index;
@end

@implementation KRRepairReportVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _index = 1;
   // [self getDeviceRepairLlist];
    [self tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDeviceRepairLlist:_type];
}

- (void)getDeviceRepairLlist:(NSInteger)type{
    WEAKSELF
    _type = type;
    NSDictionary *parameters;
    if(type == 5){
        parameters = @{@"index":@(_index),@"size":@(20)};
    }else{
        parameters = @{@"index":@(_index),@"size":@(20),@"status":@(type)};
    }
    [NetworkingTool POST:Device_repair_list parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
        if([response[@"code"] intValue] == 0){
            if(weakSelf.index == 1){
                [weakSelf.dataArrM removeAllObjects];
            }
            if([response[@"data"][@"list"] isKindOfClass:[NSArray class]]){
                NSArray *arr = response[@"data"][@"list"];
                for (int i = 0; i < arr.count; i ++) {
                    NSDictionary *dict = arr[i];
                    RepairModel *model = [RepairModel initWithDictionary:dict];
                    [weakSelf.dataArrM addObject:model];
                }
                if(arr.count < 20){
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                if (self.dataArrM.count == 0) {
                    [weakSelf.tableView.mj_footer setHidden:YES];
                }else{
                    [weakSelf.tableView.mj_footer setHidden:NO];
                }
                [weakSelf.tableView reloadData];
            }
            if(self.dataArrM.count == 0){
                KRNodataView *view = [[KRNodataView alloc] initWithFrame:CGRectZero];
                weakSelf.tableView.tableHeaderView = view;
            }else{
                weakSelf.tableView.tableHeaderView = nil;
            }
        }else{
            KRNodataView *view = [[KRNodataView alloc] initWithFrame:CGRectZero];
            weakSelf.tableView.tableHeaderView = view;
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];

    }];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RepairModel *model = self.dataArrM[indexPath.section];
    if(model.status == 0){
        return 206;
    }else{
        return 166;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RepairReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RepairReportCell"];
    RepairModel *model = self.dataArrM[indexPath.section];
    cell.titleLbl.text = @"售后服务维修单";
    cell.typeNameLbl.text = model.ebike_type_name;
   // cell.errorLbl.text = model.fault_type_name;
    cell.timeLbl.text = [[DeteManager shareInstance] timeFormatYYYYMMdd:model.created_at];
    [cell.deviceIcon sd_setImageWithURL:[NSURL URLWithString:model.ebike_type_image] placeholderImage:[UIImage imageNamed:@"devcie_defalaut_icon"]];
    cell.revokeBtn.hidden = YES;
    if(model.status == 0){
        cell.statsusIcon.image = [UIImage imageNamed:@"error_nosel_icon"];
        cell.statusLbl.text = @"待接单";
        cell.revokeBtn.hidden = NO;
    }else if (model.status == 2){
        cell.statsusIcon.image = [UIImage imageNamed:@"messge_gj_icon"];
        cell.statusLbl.text = @"已完成";
        cell.revokeBtn.hidden = YES;
    }else if(model.status == 1){
        cell.statsusIcon.image = [UIImage imageNamed:@"message_notice_icon"];
        cell.statusLbl.text = @"进行中";
        cell.revokeBtn.hidden = YES;
    }else{
        cell.statsusIcon.image = [UIImage imageNamed:@""];
        cell.statusLbl.text = @"已撤销";
        cell.revokeBtn.hidden = YES;
    }
    cell.revokeBtn.tag = indexPath.section;
    [cell.revokeBtn addTarget:self action:@selector(chexiao:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    RepairModel *model = self.dataArrM[indexPath.section];
    KRRepairReportDetailVC *vc = [[KRRepairReportDetailVC alloc] init];
    vc.model = model;
    vc.type = 1001;
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)chexiao:(UIButton *)sender{
    WEAKSELF
    RepairModel *model = self.dataArrM[sender.tag];
    [NetworkingTool POST:@"/v1/app/repair/recall" parameters:@{@"id":@(model.Id)} isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue] == 0){
            [weakSelf getDeviceRepairLlist:weakSelf.type];
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {

    }];
}


- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0
                                                                   , DEVICE_WIDTH, self.view.frame.size.height - 60 - NAVTOP_Height)];
        _tableView.backgroundColor = Main_BackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 206;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"RepairReportCell" bundle:nil] forCellReuseIdentifier:@"RepairReportCell"];
        WEAKSELF
        _tableView.mj_header =
        [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.index = 1;
            [self getDeviceRepairLlist:weakSelf.type];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index ++;
            [self getDeviceRepairLlist:weakSelf.type];
        }];
        _tableView.mj_footer.hidden = YES;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArrM{
    if(!_dataArrM){
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}
@end
